const { addAddressService, getAllAddressService, deleteAddressService, getAddressService, updateAddressService } = require('../service/address.service')
const errorType = require('../constant/errorType')
class AddressController {
  async add_address(ctx, next) {
    const address = ctx.request.body
    const res = await addAddressService(address)
    if (res) {
      ctx.body = {
        status: 200,
        msg: 'ok'
      }
    }
  }
  async get_all_address(ctx, next) {
    console.log(ctx.request.body);
    const res = await getAllAddressService(ctx.request.body.userId)
    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        data: res,
        msg: 'ok'
      }
    } else {
      return ctx.app.emit('error', new Error(errorType.ADDRESS_IS_EMPTY), ctx)
    }
  }
  async delete_address(ctx, next) {
    const { id } = ctx.request.body
    const res = await deleteAddressService(id)
    if (res) {
      ctx.body = {
        status: 200,
        msg: '删除成功'
      }
    }
  }
  async get_address(ctx, next) {
    const { id } = ctx.request.body
    const res = await getAddressService(id)
    ctx.body = {
      status: 200,
      data: res,
      msg: 'ok'
    }
  }
  async update_address(ctx, next) {
    const res = await updateAddressService(ctx.request.body.address)
    ctx.body = {
      status: 200,
      data: res,
      msg: 'ok'
    }
  }
}


module.exports = new AddressController()